#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   __init__.py
@Time    :   2024-07-09 13:05:19
@Author  :   chakcy 
@Email   :   947105045@qq.com
@description   :   config file
'''

import yaml
from .database_config import DatabaseConfig
from .jwt_config import JWTConfig
from .redis_config import RedisConfig
from .nginx import NginxConfig

class Config:
    """配置类"""
    def __init__(self, config_file:str):
        with open(config_file, 'r') as f:
            self.config = yaml.safe_load(f)["app"]
        self.name = self.config.get('name', 'default_name')  
        self.version = self.config.get('version', 'default_version')  
        self.description = self.config.get('description', 'No description provided')  
        self.author = self.config.get('author', 'Unknown Author')  
        self.environment = self.config.get('environment', [])  
        self.host = self.config.get('host', '0.0.0.0')  
        self.port = self.config.get('port', 8000)  
        self.reload = self.config.get('reload', False)
        self.workers = self.config.get('workers', 1)
        self.upload_folder = self.config.get('upload_folder', 'uploads')

        self.nginx = NginxConfig(
            server_name=self.config.get('nginx', {}).get('server_name', self.host),
            server_port=self.config.get('nginx', {}).get('server_port', self.port),
        )

        self.database = DatabaseConfig(            
            host=self.config.get('database', {}).get('host', 'localhost'),  
            port=self.config.get('database', {}).get('port', 3306),  
            username=self.config.get('database', {}).get('username', 'root'),  
            password=self.config.get('database', {}).get('password', ''),  
            database_name=self.config.get('database', {}).get('database', 'default_db'),  
            pool_size=self.config.get('database', {}).get('pool_size', 5),  
            max_overflow=self.config.get('database', {}).get('max_overflow', 10),
        )
        self.jwt = JWTConfig(
            secret_key=self.config.get('jwt', {}).get('secret_key', 'default_secret_key'),  
            algorithm=self.config.get('jwt', {}).get('algorithm', 'HS256'),  
            access_token_expire_minutes=self.config.get('jwt', {}).get('access_token_expire_minutes', 60),
        )
        self.redis = RedisConfig(
            host=self.config.get('redis', {}).get('host', 'localhost'),  
            port=self.config.get('redis', {}).get('port', 6379),  
            password=self.config.get('redis', {}).get('password', ''), 
        )
    
config = Config("application.yml")

__all__ = ["config"]